package backgammon;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Jacob Kwitkoski
 * @version 1.0
 */
public class StatPanel extends JPanel implements ActionListener {

    private Color bgColor = Color.WHITE;

    private Dice dice;

    private int die1;

    private int die2;

    private int localPips;

    private int remotePips;

    private boolean localTurn;

    private JButton roll;

    private JButton undoMove;

    private GameLogic logic;

    public StatPanel(GameLogic newLogic) {
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(layout);

        logic = newLogic;
        localTurn = true;

        dice = new Dice();
        roll = new JButton("roll");
        undoMove = new JButton("undo move");
        roll.addActionListener(this);

        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.5;
        c.weighty = 1.0;
        add(roll, c);

    }

    @Override
    public final void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setBackground(bgColor);
        g.setColor(Color.BLACK);
        g.drawString("Local Player Pips: " + localPips, 20, 50);
        g.drawString("Local pieces on bar: " + logic.localPlayer.hasPiecesOnBar(), 20, 70);
        g.drawString("Remote Player Pips: " + remotePips, 20, 100);
        //g.drawString("Remote pieces on bar: " + logic.remotePlayer.hasPiecesOnBar(), 20, 120);
        g.drawString("die1: " + die1 + "\t\tused: " + dice.isDie1used(), 20, 150);
        g.drawString("die2: " + die2 + "\t\tused: " + dice.isDie2used(), 20, 170);
        if (localTurn) {
            g.drawString("Local player's turn", 20, 200);
        } else {
            g.drawString("Remote player's turn", 20, 200);
        }
    }

    public final void setLocalPips(final int newLocalPips) {
        localPips = newLocalPips;
    }

    public final void setRemotePips(final int newRemotePips) {
        remotePips = newRemotePips;
    }

    public final Dice getDice() {
        return dice;
    }

    public final void actionPerformed(final ActionEvent e) {
        dice.roll();
        die1 = dice.getDie1();
        die2 = dice.getDie2();
        roll.setEnabled(false);
        logic.processMovesForRoll();
        repaint();
    }

    public final boolean isLocalTurn() {
        return localTurn;
    }

    public final void setLocalTurn(final boolean newLocalTurn) {
        localTurn = newLocalTurn;
    }

    public void enableRoll() {
        roll.setEnabled(true);
        repaint();
    }
}
